// 订单相关接口

const express = require("express")
const router = express.Router()
const pool = require("../../db/db.js")
const Response = require("../../utils/ResponseData.js")

router.post("/add", (req, res, next) => {
    let { uid } = req.user
    console.log(req.body);
    let { pet_id, bus_id, po_price, po_beizhu, po_number, po_status, po_time, c_id } = req.body
    // 生成不重复订单编号
    po_number = "OP" + +new Date()
    const sql = "insert into pet_po(pet_id, bus_id, po_price, po_beizhu, po_number, po_status, po_time, c_id, u_id) values(?,?,?,?,?,?,?,?,?)"
    pool.query(sql, [pet_id, bus_id, po_price, po_beizhu, po_number, po_status, po_time, c_id, uid], (err, result) => {
        if (err) {
            console.log("新增订单错误", err);
            next(err)
            return
        }
        if (result && result.insertId > 0) {
            res.send(Response.success(null, "预定成功"))
        } else {
            res.send(Response.error("预定失败"))
        }
    })
})

module.exports = router